/**
 * Created by liubin on 2017/6/2.
 */


var DtjTableUILayer = GameCCSBase.extend({

    mDtjTableSettingLayer : null,
    mDtjTableChatLayer : null,
    mDtjTableEndLayer : null,
    mDtjTableMicLayer : null,

    Panel_hostBlack : null,
    Panel_playeAnim : null,
    mDtjTablePlayerInfoLayer : null,

    panel_ready : null,
    Panel_opration : null,
    Panel_bet : null,
    Panel_guo : null,
    Panel_guoBet : null,
    Panel_host : null,

    Button_invite : null,
    Button_start : null,
    Button_disband : null,

    mPlayer : null,
    mPlayerPoss : null,
    mCardTypes : null,
    mQiangzhuangImages : null,

    //网络延迟
    mKwxSysInfoWidget : null,
    Button_kick1 : null,
    Button_kick2 : null,
    Button_kick3 : null,

    mProgressTimer : null,
    Panel_time : null,
    Image_timeBg : null,
    mTimeLeft : null,

    //同ip提醒
    Image_ipwarring : null,

    //开牌
    openBtn : null,

    //提示
    tishiBtn : null,

    //下注
    mButton_bets : null,
    mBetNum : null,
    mBetRang : null,
    mBetIndex : null,

    mGuoBetNum : null,

    ctor : function() {
        this._super();
        this.loadCocostudio("res/ccs/DtjTableUILayer.json");


        this.panel_ready = mo.seekWidgetByName(this.ccsNode, "Panel_ready");
        this.Panel_opration = mo.seekWidgetByName(this.ccsNode, "Panel_opration");
        this.Panel_bet = mo.seekWidgetByName(this.ccsNode, "Panel_bet");
        this.Panel_host = mo.seekWidgetByName(this.ccsNode, "Panel_host");
        this.Panel_playeAnim = mo.seekWidgetByName(this.ccsNode, "Panel_playeAnim");
        this.Panel_hostBlack = mo.seekWidgetByName(this.ccsNode, "Panel_hostBlack");
        this.Panel_guo = mo.seekWidgetByName(this.ccsNode, "Panel_guo");
        this.Panel_guoBet = mo.seekWidgetByName(this.Panel_guo, "Panel_guoBet");

        this.Button_invite = mo.seekWidgetByName(this.ccsNode, "Button_invite");
        this.Button_start = mo.seekWidgetByName(this.ccsNode, "Button_start");
        this.Button_disband = mo.seekWidgetByName(this.ccsNode, "Button_disband");

        this.Button_kick1 = mo.seekWidgetByName(this.ccsNode, "Button_kick1");
        this.Button_kick2 = mo.seekWidgetByName(this.ccsNode, "Button_kick2");
        this.Button_kick3 = mo.seekWidgetByName(this.ccsNode, "Button_kick3");
        this.Image_ipwarring = mo.seekWidgetByName(this.ccsNode, "Image_ipwarring");

        this.Panel_time = mo.seekWidgetByName(this.ccsNode, "Panel_time");
        this.Image_timeBg = mo.seekWidgetByName(this.ccsNode, "Image_timeBg");

        this.tishiBtn = mo.seekWidgetByName(this.ccsNode, "Button_tishi");
        this.openBtn = mo.seekWidgetByName(this.ccsNode, "Button_kaipai");

        this.openBtn.setBright(false);
        this.openBtn.setTouchEnabled(false);


        this.loadUI();
    },



    onEnter : function(){
        this._super();

        mo.eventHelper.add("UserChatMsgResponse", this, function(userChatMsgResponse){
            this.setChatMsg(userChatMsgResponse);
        });

        mo.eventHelper.add("UserOnLineChange", this, function(onlineInfo){
            this.setPlayerOnlineStats(onlineInfo);
        });

        mo.eventHelper.add("UserChatMsgResponse_reward", this, function(userChatMsgResponse){
            this.setChatMsgReward(userChatMsgResponse);
        });
    },

    onExit : function(){
        mo.eventHelper.remove(this);
        this._super();
    },

    loadUI : function () {

        if (cc.sys.isNative){
            this.mDtjTableMicLayer = new DtjTableMicLayer();
            this.addChild(this.mDtjTableMicLayer);
        }

        //个人信息
        this.mDtjTablePlayerInfoLayer = new KwxTablePlayerInfoLayer();
        this.addChild(this.mDtjTablePlayerInfoLayer);

        this.mDtjTableSettingLayer = new KwxDialogSetting("dtj");
        this.addChild(this.mDtjTableSettingLayer);

        //聊天窗口
        this.mDtjTableChatLayer = new KwxDialogChat();
        this.mDtjTableChatLayer.setChatButton(mo.seekWidgetByName(this.ccsNode, "Button_chat"), "dtj");
        this.addChild(this.mDtjTableChatLayer);


        this.mDtjTableEndLayer = new DtjTableEndLayer();
        this.addChild(this.mDtjTableEndLayer);

        //规则
        mo.seekWidgetByName(this.ccsNode, "Button_rule").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.addChild(new DtjTableRuleLayer());
            }
        }, this);

        //设置
        mo.seekWidgetByName(this.ccsNode, "Button_setting").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.mDtjTableSettingLayer.setDialog(true);
            }
        }, this);

        //准备
        this.Button_start.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                mo.gameMsgManager.sendGameReadyRequestdtj();
            }
        }, this);

        //邀请
        this.Button_invite.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                mo.dtjTable.onInvite();
            }
        }, this);

        //退出
        this.Button_disband.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                var isStart = mo.dtjTable.mGameConfig.maxpan != mo.dtjTableStatus.mTableStatus.LeftPlayTimes;
                if (!isStart){
                    mo.showMsgDialogOkAndCancel("确定退出房间吗？\n牌局未开始退出不会消耗房卡\n房主退出会解散牌桌", function(){
                        if (mo.dtjTable.mGameConfig.maxpan == mo.dtjTableStatus.mTableStatus.LeftPlayTimes){
                            mo.gameMsgManager.sendForceExitTableRequestdtj();
                        }
                    }, this, function(){}, this);
                }
            }
        }, this);


        //开牌
        this.openBtn.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.openMyCard();
            }
        }, this);

        this.tishiBtn.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.showTishiCardGroup();
            }
        }, this);

        //用户
        this.mPlayer = [];
        this.mPlayerPoss = [];
        var Panel_player = mo.seekWidgetByName(this.ccsNode, "Panel_player");
        for(var i = 0; i < 4; i++){
            var player = mo.seekWidgetByName(Panel_player, "Panel_player" + i);
            var playerItem = new DtjTablePlayer(i);
            player.addChild(playerItem);
            this.mPlayer.push(playerItem);

            this.mPlayerPoss.push(cc.pAdd(player.getPosition(), cc.p(0, 45)));
        }

        //手牌位置
        mo.dtjCardHelper.cardInhandPoints = [];
        this.mCardTypes = [];
        this.mQiangzhuangImages = [];
        var Panel_cardPos = mo.seekWidgetByName(this.ccsNode, "Panel_cardPos");
        for(var i=0; i<4; i++){
            var cardPos = mo.seekWidgetByName(Panel_cardPos, "Panel_cardPos" + i);
            mo.dtjCardHelper.cardInhandPoints.push(cardPos);

            var Image_cardType1 = mo.seekWidgetByName(cardPos, "Image_cardType1");
            var Image_cardType2 = mo.seekWidgetByName(cardPos, "Image_cardType2");

            Image_cardType1.setVisible(false);
            Image_cardType2.setVisible(false);

            this.mCardTypes.push([Image_cardType1, Image_cardType2]);

            var Image_qianghost = mo.seekWidgetByName(cardPos, "Image_qianghost");
            this.mQiangzhuangImages.push(Image_qianghost);

        }

        //下注
        var betNums = [1, 2, 5, 8, 10];
        this.mButton_bets = [];
        this.mBetNum = [];
        for (var i=0; i<5; i++){
            var Button_bet = mo.seekWidgetByName(this.Panel_bet, "Button_bet" + i);
            Button_bet.setVisible(false);
            this.mButton_bets.push(Button_bet);

            Button_bet.setTag(i);
            Button_bet.addTouchEventListener(function(sender, type){
                if (mo.gameHelper.getIsTouchEnd(type)) {
                    var betNum = betNums[sender.getTag()];

                    this.mBetNum.push(betNum);

                    if(this.mBetNum.length == 2){
                        mo.dtjTableStatus.sendMyBet(this.mBetNum);
                        this.Panel_bet.setVisible(false);
                    }else{
                        this.showBetButton();
                    }

                }
            }, this);
        }


        //锅
        this.Panel_guoBet = mo.seekWidgetByName(this.Panel_guo, "Panel_guoBet");
        for (var i=0; i<4; i++){
            var Button_bet = mo.seekWidgetByName(this.Panel_guoBet, "Button_bet" + i);
            Button_bet.setVisible(false);
            this.mButton_bets.push(Button_bet);

            Button_bet.setTag(i);
            Button_bet.addTouchEventListener(function(sender, type){
                if (mo.gameHelper.getIsTouchEnd(type)) {
                    var beiNum = this.mBetRang[0] + 5 * sender.getTag();
                    this.mGuoBetNum = beiNum;

                    this.mBetNum.push(beiNum);

                    if(this.mBetNum.length == 2){
                        mo.dtjTableStatus.sendMyBet(this.mBetNum);
                        this.Panel_guoBet.setVisible(false);
                    }else{
                        this.showGuoBetButton();
                    }

                }
            }, this);
        }

        //切锅
        var Button_qieguo_yes = mo.seekWidgetByName(this.Panel_guo, "Button_qieguo_yes");
        Button_qieguo_yes.addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                mo.gameMsgManager.sendActionRequestdtj(3, 1, []);
            }
        }, this);

        var Button_qieguo_no = mo.seekWidgetByName(this.Panel_guo, "Button_qieguo_no");
        Button_qieguo_no.addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                mo.gameMsgManager.sendActionRequestdtj(3, 0, []);
            }
        }, this);

        //续锅
        var Button_xuguo_yes = mo.seekWidgetByName(this.Panel_guo, "Button_xuguo_yes");
        Button_xuguo_yes.addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                mo.gameMsgManager.sendActionRequestdtj(4, 1, []);
            }
        }, this);

        var Button_xuguo_no = mo.seekWidgetByName(this.Panel_guo, "Button_xuguo_no");
        Button_xuguo_no.addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                mo.gameMsgManager.sendActionRequestdtj(4, 0, []);
            }
        }, this);


        //踢人
        this.Button_kick1.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.setKickPlayerForViewSeat(1);
            }
        }, this);

        this.Button_kick2.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.setKickPlayerForViewSeat(2);
            }
        }, this);
        this.Button_kick3.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.setKickPlayerForViewSeat(3);
            }
        }, this);


        //抢庄
        var qiangzhuang = mo.seekWidgetByName(this.Panel_host, "Button_qiang");
        qiangzhuang.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                mo.dtjTableStatus.sendMakeHost(1);
                this.setMakeHost(false);
            }
        }, this);

        var buqiangzhuang = mo.seekWidgetByName(this.Panel_host, "Button_buqiang");
        buqiangzhuang.addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)) {
                mo.dtjTableStatus.sendMakeHost(0);
                this.setMakeHost(false);
            }
        }, this);


        //倒计时
        this.mProgressTimer = new cc.ProgressTimer(new cc.Sprite("res/ccs/dtj/leftTime_bg1.png"));
        this.mProgressTimer.type = cc.ProgressTimer.TYPE_RADIAL;
        this.mProgressTimer.setReverseDirection(true);
        this.mProgressTimer.setPosition(cc.p(this.Image_timeBg.getContentSize().width / 2, this.Image_timeBg.getContentSize().height / 2));
        this.Image_timeBg.addChild(this.mProgressTimer);


        //wifi控件
        this.mKwxSysInfoWidget = new KwxSysInfoWidget();
        mo.seekWidgetByName(this.ccsNode, "Panel_wifi").addChild(this.mKwxSysInfoWidget);
    },

    reset : function () {
        this.openBtn.setBright(false);
        this.openBtn.setTouchEnabled(false);

        this.mBetNum = [];
        for (var i=0; i<this.mCardTypes.length; i++){
            this.mCardTypes[i][0].setVisible(false);
            this.mCardTypes[i][1].setVisible(false);
        }

    },


    //设置牌桌用户信息
    setTablePlayerInfos : function(infos){
        this.mPlayerInfos = infos;
        for (var i = 0; i < 4; i++){
            this.mPlayer[i].setPlayerInfo(infos[i]);
        }
    },

    setGuoState : function () {
        //没锅
        if(mo.dtjTable.mGameConfig.guo == 0){
            this.Panel_guo.setVisible(false);
        }else { //有锅
            this.Panel_guo.setVisible(true);
        }
    },

    setQieGuoOrXuguoState : function (show, isHost, newAction) {

        if(!show){
            mo.seekWidgetByName(this.Panel_guo, "Panel_qieguo").setVisible(false);
            mo.seekWidgetByName(this.Panel_guo, "Panel_xuguo").setVisible(false);
            mo.seekWidgetByName(this.Panel_guo, "Image_hostguo").setVisible(false);
            return;
        }

        if(isHost){
            mo.seekWidgetByName(this.Panel_guo, "Panel_qieguo").setVisible(newAction == 3);
            mo.seekWidgetByName(this.Panel_guo, "Panel_xuguo").setVisible(newAction == 4);
        }else{
            var Image_hostguo = mo.seekWidgetByName(this.Panel_guo, "Image_hostguo");
            Image_hostguo.setVisible(true);

            if(newAction == 3){
                mo.gameHelper.loadTextureImage(Image_hostguo, "res/ccs/dtj/host_qieguo.png");
            }else{
                mo.gameHelper.loadTextureImage(Image_hostguo, "res/ccs/dtj/host_xuguo.png");
            }
        }
    },

    showReady : function(isShow){
        if(isShow == false){
            this.panel_ready.setVisible(false);
            return;
        }
        this.panel_ready.setVisible(true);

        //是否打了一局了
        var isPlay = mo.dtjTableStatus.mTableStatus.LeftPlayTimes != mo.dtjTable.mGameConfig.maxpan;
        this.Button_invite.setVisible(!isPlay);
        this.Button_disband.setVisible(!isPlay);

        //自己是否准备
        var myIsReady = mo.dtjTableStatus.getMyIsReady();
        if (myIsReady){
            this.mDtjTableEndLayer.setVisible(false);
        }

        //准备按钮显示
        if (!isPlay){
            //还没有开始
            if (mo.dtjTableStatus.mTableStatus.StartGameSeat == mo.dtjTableStatus.mTableStatus.MySeat){
                //自己是开房负责人
                this.Button_start.setVisible(true);
                this.Button_start.setEnabled(mo.dtjTableStatus.getPlayerCount() >= 2);
                this.Button_invite.setPositionX(mo.gameSize.x * 0.3);
                this.Button_disband.setPositionX(mo.gameSize.x * 0.7);
            }else{
                this.Button_start.setVisible(false);
                this.Button_invite.setPositionX(mo.gameSize.x * 0.4);
                this.Button_disband.setPositionX(mo.gameSize.x * 0.6);
            }
        }else{
            //已经开始
            this.Button_start.setPositionX(mo.gameSize.x / 2);
            this.Button_start.setEnabled(true);
            if (isPlay){
                this.Button_start.setVisible(false);
            }else{
                this.Button_start.setVisible(!myIsReady);
            }
        }

        //准备按钮上面的文字
        if (!isPlay){
            this.Button_start.loadTextures("res/ccs/dtj/btn/btn_zhunbei1.png", "res/ccs/dtj/btn/btn_zhunbei2.png");
        }else{
            this.Button_start.loadTextures("res/ccs/dtj/btn/btn_jixu1.png", "res/ccs/dtj/btn/btn_jixu2.png");
        }

        //处理同ip显示问题
        var ipEquals = mo.dtjTableStatus.getPlayerIpEqual();
        if (ipEquals.length > 0 && !isPlay){
            this.Image_ipwarring.setVisible(true);
            for (var i = 0; i < 4; i++){
                var Panel_ip = mo.seekWidgetByName(this.Image_ipwarring, "Panel_ip" + i);
                if (ipEquals.length > i){
                    Panel_ip.setVisible(true);
                    mo.gameHelper.setTextString(mo.seekWidgetByName(Panel_ip, "Text_ip_nick"), ipEquals[i].nick);
                    mo.gameHelper.setTextString(mo.seekWidgetByName(Panel_ip, "Text_ip_ip"), ipEquals[i].ip);
                }else {
                    Panel_ip.setVisible(false);
                }
            }
        }else{
            this.Image_ipwarring.setVisible(false);
        }

        //能否显示踢人
        if (mo.dtjTableStatus.mTableStatus != null && mo.dtjTable.mGameConfig != null){
            var isPlay = mo.dtjTableStatus.mTableStatus.LeftPlayTimes != mo.dtjTable.mGameConfig.maxpan;
            var funcShowKick = function(viewSeat){
                if (isPlay){
                    return false;
                }

                var logicSeat = mo.dtjTableStatus.getLogicForView(viewSeat, mo.dtjTableStatus.mTableStatus.MySeat - 1) + 1;
                return mo.dtjTable.mIsRoomCreator && mo.dtjTableStatus.mTableStatus["SeatUser" + logicSeat] != null;
            };

            this.Button_kick1.setVisible(funcShowKick(1));
            this.Button_kick2.setVisible(funcShowKick(2));
            this.Button_kick3.setVisible(funcShowKick(3));
        }
    },

    //设置录像隐藏
    setIsReplay : function(isReplay){
        mo.seekWidgetByName(this.ccsNode, "Button_setting").setVisible(!isReplay);
        mo.seekWidgetByName(this.ccsNode, "Button_chat").setVisible(!isReplay);

        this.mKwxSysInfoWidget.setVisible(!isReplay);
        if (this.mDtjTableMicLayer != null){
            this.mDtjTableMicLayer.setVisible(!isReplay);
        }
    },

    //踢人
    setKickPlayerForViewSeat : function(viewSeat){
        var logicSeat = mo.dtjTableStatus.getLogicForView(viewSeat, mo.dtjTableStatus.mTableStatus.MySeat - 1) + 1;
        var SeatUser = mo.dtjTableStatus.mTableStatus["SeatUser" + logicSeat];
        if (SeatUser != null){
            mo.showMsgDialogOkAndCancel("确定要踢掉玩家[" + SeatUser.user_info.nickname + "]吗?", function(){
                mo.gameMsgManager.sendCreateUserKickOutRequestdtj(SeatUser.user_info.userid);
            }, this, function(){}, this);
        }
    },

    showOprationLayer : function () {
        if(mo.dtjTable.mPlayerType == 0){
            return;
        }
        this.Panel_opration.setVisible(true);

        if(mo.dtjTable.mGameConfig.paijiu == 1){
            this.tishiBtn.setVisible(false);
            this.openBtn.setVisible(true);

            this.openBtn.setBright(true);
            this.openBtn.setTouchEnabled(true);

            // this.openBtn.setPosition(cc.p(this.openBtn.getPositionX(), this.openBtn.getPositionY() + 40));
        }

    },

    openMyCard : function () {
        this.Panel_opration.setVisible(false);

        this.Panel_time.setVisible(false);
        this.unschedule(this.ontUpdateTimeLeft);

        mo.dtjTable.mDtjCardLayer.openMyCard();
    },

    showOpenCardType : function (seat, cardOrder) {
        var bigCardTypeIndx = mo.dtjCardHelper.getCardGroup(cardOrder[0], cardOrder[1]);
        this.mCardTypes[seat][1].setVisible(true);
        mo.gameHelper.loadTextureImage(this.mCardTypes[seat][1], "cardType" + (bigCardTypeIndx + 1) + ".png", ccui.Widget.PLIST_TEXTURE);

        if(cardOrder.length == 4){
            var smallCardTypeIndx = mo.dtjCardHelper.getCardGroup(cardOrder[2], cardOrder[3]);
            this.mCardTypes[seat][0].setVisible(true);
            mo.gameHelper.loadTextureImage(this.mCardTypes[seat][0], "cardType" + (smallCardTypeIndx + 1) + ".png", ccui.Widget.PLIST_TEXTURE);
        }else{
            this.mCardTypes[seat][1].setPositionX(0);
        }
    },

    showCardType : function (seat, cardOrder, delayTime) {

        if(!delayTime){
            delayTime = 0;
        }

        var sex = mo.dtjTableStatus.getSexForLogicSeat(seat);
        if(mo.dtjTable.mGameConfig.paijiu == 1){
            var cardTypeIndx = mo.dtjCardHelper.getCardGroup(cardOrder[0], cardOrder[1]);
            this.runAction(cc.sequence(
                cc.delayTime(delayTime),
                cc.callFunc(function () {
                    this.mCardTypes[seat][0].setVisible(true);
                    this.mCardTypes[seat][0].setPositionX(0);
                    mo.gameHelper.loadTextureImage(this.mCardTypes[seat][0], "cardType" + (cardTypeIndx + 1) + ".png", ccui.Widget.PLIST_TEXTURE);
                    mo.soundHelper.playDtjCardType(sex, cardTypeIndx+1);

                }, this)
            ));
        }else{
            var bigCardTypeIndx = mo.dtjCardHelper.getCardGroup(cardOrder[0], cardOrder[1]);
            var smallCardTypeIndx = mo.dtjCardHelper.getCardGroup(cardOrder[2], cardOrder[3]);
            if(seat == 0){
                this.runAction(cc.sequence(
                    cc.delayTime(delayTime),
                    cc.delayTime(1.5),
                    cc.callFunc(function () {
                        this.mCardTypes[seat][1].setVisible(true);
                        mo.gameHelper.loadTextureImage(this.mCardTypes[seat][1], "cardType" + (bigCardTypeIndx + 1) + ".png", ccui.Widget.PLIST_TEXTURE);
                        mo.soundHelper.playDtjCardType(sex, bigCardTypeIndx+1);

                    }, this),
                    cc.delayTime(1),
                    cc.callFunc(function () {
                        this.mCardTypes[seat][0].setVisible(true);
                        mo.gameHelper.loadTextureImage(this.mCardTypes[seat][0], "cardType" + (smallCardTypeIndx + 1) + ".png", ccui.Widget.PLIST_TEXTURE);
                        mo.soundHelper.playDtjCardType(sex, smallCardTypeIndx+1);
                    }, this)
                ));
            } else {
                this.runAction(cc.sequence(
                    cc.delayTime(delayTime),
                    cc.delayTime(0.5),
                    cc.callFunc(function () {
                        this.mCardTypes[seat][1].setVisible(true);
                        mo.gameHelper.loadTextureImage(this.mCardTypes[seat][1], "cardType" + (bigCardTypeIndx + 1) + ".png", ccui.Widget.PLIST_TEXTURE);
                        mo.soundHelper.playDtjCardType(sex, bigCardTypeIndx+1);
                    }, this),
                    cc.delayTime(2.5),
                    cc.callFunc(function () {
                        this.mCardTypes[seat][0].setVisible(true);
                        mo.soundHelper.playDtjCardType(sex, smallCardTypeIndx+1);
                        mo.gameHelper.loadTextureImage(this.mCardTypes[seat][0], "cardType" + (smallCardTypeIndx + 1) + ".png", ccui.Widget.PLIST_TEXTURE);
                    }, this)
                ));
            }
        }
    },

    //显示提示的牌型组合
    showTishiCardGroup : function () {
        mo.dtjTable.mDtjCardLayer.showTishiCardGroup();
    },


    //设置加倍次数
    setBetInfo : function(newAction){
        if(mo.dtjTable.mPlayerType == 0){
            return;
        }

        this.runAction(cc.sequence(
            cc.delayTime(1.0),
            cc.callFunc(function () {
                //下注
                mo.dtjTableStatus.sendMyBet([10, 10]);
            }, this)
        ));

        // this.mBetIndex = 0;
        // this.mBetRang = newAction[0].Cards;
        // this.mBetNum = [];
        //
        // if(mo.dtjTable.mGameConfig.guo == 1){
        //     this.Panel_guoBet.setVisible(true);
        //     this.showGuoBetButton();
        // }else{
        //     this.Panel_bet.setVisible(true);
        //     this.showBetButton();
        // }
    },

    showBetButton : function () {
        var Image_betState = mo.seekWidgetByName(this.Panel_bet, "Image_betState");
        mo.gameHelper.loadTextureImage(Image_betState, "res/ccs/dtj/betState" + (3 - this.mBetIndex)+ ".png");
        this.mBetIndex = 1;

        var betNums = [1, 2, 5, 8, 10];

        var betBtns = [];
        for (var i = 0; i < 5; i++) {
            var btn = this.mButton_bets[i];
            betBtns.push(btn);
            btn.setVisible(true);
            var beiNum = betNums[i];
            var bet_num = mo.seekWidgetByName(btn, "bet_num");
            bet_num.ignoreContentAdaptWithSize(true);
            bet_num.setString(beiNum+ "");
        }

        var startP = -(betBtns.length - 1) * 120 / 2;
        for (var i = 0; i < betBtns.length; i++){
            betBtns[i].setPositionX(startP + 120 * i);
        }
    },

    showGuoBetButton : function () {
        if(this.mBetIndex == 1){
            this.mBetRang[1] = this.mBetRang[1] - this.mGuoBetNum;
            this.mBetRang[0] = 0;
        }else {
            if(this.mBetRang[1] < this.mBetRang[0]){
                this.mBetRang[1] = this.mBetRang[0];
            }
        }

        var Image_betState = mo.seekWidgetByName(this.Panel_guoBet, "Image_betState");
        mo.gameHelper.loadTextureImage(Image_betState, "res/ccs/dtj/betState" + (3 - this.mBetIndex)+ ".png");
        this.mBetIndex = 1;

        var betBtns = [];
        for (var i = 0; i < 4; i++) {
            var btn = this.mButton_bets[i+5];
            var beiNum = this.mBetRang[0] + 5 * i;

            if(beiNum > this.mBetRang[1]){
                btn.setVisible(false);
                continue;
            }

            betBtns.push(btn);
            btn.setVisible(true);

            var bet_num = mo.seekWidgetByName(btn, "bet_num");
            bet_num.ignoreContentAdaptWithSize(true);
            bet_num.setString(beiNum+ "");
        }

        var startP = -(betBtns.length - 1) * 120 / 2;
        for (var i = 0; i < betBtns.length; i++){
            betBtns[i].setPositionX(startP + 120 * i);
        }

    },

    // showGuoBetSlider : function () {
    //
    //     //
    //     if(this.mBetIndex == 1){
    //         this.mBetRang[1] = this.mBetRang[1] - this.mGuoBetNum;
    //         this.mBetRang[0] = 0;
    //     }else {
    //         if(this.mBetRang[1] < this.mBetRang[0]){
    //             this.mBetRang[1] = this.mBetRang[0];
    //         }
    //     }
    //
    //     var Image_betState = mo.seekWidgetByName(this.Panel_guoBet, "Image_betState");
    //     mo.gameHelper.loadTextureImage(Image_betState, "res/ccs/dtj/betState" + (3 - this.mBetIndex)+ ".png");
    //     this.mBetIndex = 1;
    //
    //     var guo_betSlider = mo.seekWidgetByName(this.Panel_guoBet, "guo_betSlider");
    //     guo_betSlider.setPercent(0);
    //
    //     this.updateGuoBetNum(0);
    // },
    //
    // updateGuoBetNum : function (percent) {
    //     this.mGuoBetNum = this.mBetRang[0] + parseInt((this.mBetRang[1] - this.mBetRang[0]) * (percent / 100));
    //     if(this.mGuoBetNum < this.mBetRang[0]){
    //         this.mGuoBetNum = this.mBetRang[0]
    //     }
    //     var bet_num_atlas = mo.seekWidgetByName(this.Panel_guoBet, "bet_num");
    //     bet_num_atlas.setString(this.mGuoBetNum + "");
    // },

    //输赢金币动画
    setScoreAnim : function(scores, hostSeat){
        this.Panel_playeAnim.removeAllChildren();

        if(mo.dtjTable.mGameConfig.guo == 1){
            var Image_icon_pos = mo.seekWidgetByName(this.Panel_guo, "Image_icon").getPosition();
            this.mPlayerPoss[hostSeat] = cc.p(Image_icon_pos.x, Image_icon_pos.y);
        }

        var delayTime = 1 + 4;
        if(mo.dtjTable.mGameConfig.paijiu == 1){
            delayTime = 1;
        }

        //动画全
        for (var seat = 0; seat < 4; seat++){
            var score = scores[seat];
            if (score == -9999){
                continue;
            }

            this.mPlayer[seat].showAddScoreAnim(score, delayTime);

            if (seat != hostSeat){
                if (score > 0){
                    this.setScoreMovoTo(hostSeat, seat, delayTime);
                }else if(score < 0){
                    this.setScoreMovoTo(seat, hostSeat, delayTime);
                }
            }
        }
        return delayTime + 1;
    },

    //飞金币
    setScoreMovoTo : function(formSeat, toSeat, delayTime){

        var coins = [];
        for (var i = 0; i < 4; i++){
            var coin = new cc.Sprite("res/ccs/dtj/gold.png");
            coin.setScale(0.6);
            coin.setVisible(false);
            this.Panel_playeAnim.addChild(coin);

            var posOffset = cc.p(Math.random() * 30 - 15, Math.random() * 30 - 15);
            var formPos = cc.pAdd(this.mPlayerPoss[formSeat], posOffset);
            var toPos = cc.pAdd(this.mPlayerPoss[toSeat], posOffset);
            var distance = cc.pDistance(formPos, toPos);
            var speed = 2000;
            time = distance / speed + i * 0.05 + delayTime;

            coin.setPosition(formPos);
            coin.runAction(cc.sequence(
                cc.delayTime(i * 0.05 + delayTime),
                cc.callFunc(function(){
                    mo.soundHelper.playDtjGetCoins();
                }, coin),
                cc.show(),
                cc.moveTo(0.5, toPos),
                cc.hide()
            ));
        }
    },


    setChatMsg : function(userChatMsg){
        var seat = mo.dtjTableStatus.getViewSeatForUid(userChatMsg.uid);
        var Panel_chat = this.mPlayer[seat].Panel_chat;
        Panel_chat.stopAllActions();

        var Image_chat = mo.seekWidgetByName(Panel_chat, "Image_chat");
        var Panel_audio = mo.seekWidgetByName(Panel_chat, "Panel_audio");
        var Text_content = mo.seekWidgetByName(Panel_chat, "Text_content");
        var Sprite_face = mo.seekWidgetByName(Panel_chat, "Sprite_face");

        Panel_chat.setVisible(false);
        Image_chat.setVisible(false);
        Sprite_face.setVisible(false);
        Panel_audio.setVisible(false);
        Text_content.setVisible(false);

        if (userChatMsg.type == 1){
            //处理文字消息
            var numIndex = parseInt(userChatMsg.msg);

            if (numIndex >=0 && numIndex < 13){
                //播放音效
                var sex = mo.dtjTableStatus.getSexForUid(userChatMsg.uid);
                mo.soundHelper.playUserCustom(sex, numIndex);

                //快捷短语
                Text_content.setVisible(true);
                mo.gameHelper.setTextString(Text_content, mo.gameUserInfo.userCustomText[numIndex]);
                var sizeW = Text_content.getContentSize().width + 40;
                sizeW = Math.max(sizeW, 70);
                Text_content.setPositionX(sizeW / 2);

                Panel_chat.setVisible(true);
                Image_chat.setVisible(true);
                Image_chat.setContentSize(cc.size(sizeW, 64));

                Panel_chat.runAction(
                    cc.sequence(
                        cc.delayTime(3.0),
                        cc.hide()
                    )
                );
            }else if (numIndex >= 51 && numIndex < 80){

                //表情
                Sprite_face.setVisible(true);
                var animation = new cc.Animation(mo.dtjCardHelper.getFaceFrames(numIndex - 51));
                animation.setDelayPerUnit(0.2);
                Sprite_face.stopAllActions();
                Sprite_face.runAction(cc.repeatForever(cc.animate(animation)));

                Panel_chat.setVisible(true);

                Panel_chat.runAction(
                    cc.sequence(
                        cc.delayTime(3.0),
                        cc.hide(),
                        cc.callFunc(function(){
                            Sprite_face.stopAllActions();
                        }, Panel_chat)
                    )
                );
            }else{
                Panel_chat.setVisible(false);

                var formSeat = seat;
                var toSeat = mo.dtjTableStatus.getViewForLogic(numIndex % 10, mo.dtjTableStatus.mTableStatus.MySeat - 1);
                var index = (parseInt(numIndex / 10) % 10);
                var p1 = this.mPlayerPoss[formSeat];
                var p2 = this.mPlayerPoss[toSeat];

                this.mDtjTablePlayerInfoLayer.showEffectChat(formSeat, toSeat, index, p1, p2);
            }
        }else{
            if (!cc.sys.isNative){
                return;
            }

            //语音
            Panel_audio.setVisible(true);

            Panel_chat.setVisible(true);
            Image_chat.setVisible(true);
            Image_chat.setContentSize(cc.size(70, 64));

            //语音信息
            var intArray = [];
            var vmsg = userChatMsg.vmsg;
            var lenght = vmsg.limit - vmsg.offset;
            for (var i = 0; i < lenght; i++){
                intArray.push(vmsg.view[i + vmsg.offset]);
            }

            var millisecond = mo.gameHelper.getMillisecond().toString();
            var fileName = millisecond + ".amr";
            mo.nativeHelper.playFile(fileName, intArray);

            var time = (lenght / 1500);
            Panel_chat.runAction(
                cc.sequence(
                    cc.delayTime(time),
                    cc.hide()
                )
            );
        }
    },

    //设置离线状态
    setPlayerOnlineStats : function(onlineInfo){
        //重置
        for (var i = 0; i < 4; i++) {
            this.mPlayer[i].setOnlineStatus(false);
        }

        //设置状态
        if (this.mPlayerInfos != null){
            for (var i = 0; i < 4; i++) {
                if (this.mPlayerInfos[i] == null){
                    continue;
                }
                var uid = this.mPlayerInfos[i].user_info.userid;

                if (onlineInfo[uid.toString()] != undefined && onlineInfo[uid.toString()] == 0){
                    this.mPlayer[i].setOnlineStatus(true);
                }
            }
        }
    },


    startTimeLeft : function () {
        this.mTimeLeft = 15.0;
        this.Panel_time.setVisible(true);
        this.schedule(this.ontUpdateTimeLeft);
    },

    //倒计时
    ontUpdateTimeLeft : function(dt){
        this.mTimeLeft -= dt;
        if (this.mTimeLeft <= 0){
            this.mTimeLeft = 0;
            //发送结束消息

            this.unschedule(this.ontUpdateTimeLeft);
        }
        mo.seekWidgetByName(this.Image_timeBg, "BitmapFontLabel_time").setString("" + parseInt(this.mTimeLeft));
        var percent = this.mTimeLeft / 15.0;
        this.mProgressTimer.setPercentage(percent * 100);
    },

    setMakeHost : function (state) {
        // this.Panel_host.setVisible(state);


        this.runAction(cc.sequence(
            cc.delayTime(1.0),
            cc.callFunc(function () {
                //抢
                mo.dtjTableStatus.sendMakeHost(1);
            }, this)
        ));

    },

    //显示抢庄图片 -1不显示，0不抢  1抢
    showMakeHostImage : function (seat, state) {
        if(state == -1){
            this.mQiangzhuangImages[seat].setVisible(false);
        }else{
            this.mQiangzhuangImages[seat].setVisible(true);

            if(state == 0){
                mo.gameHelper.loadTextureImage(this.mQiangzhuangImages[seat], "res/ccs/dtj/buqiangzhunag.png");
            }else{
                mo.gameHelper.loadTextureImage(this.mQiangzhuangImages[seat], "res/ccs/dtj/qiangzhunag.png");
            }
        }
    },

    //抢庄动画
    playMakeHostAni : function (hostSeat, makeHost, mySeatUser) {
        this.Panel_hostBlack.setVisible(true);
        var repeatTime = makeHost.length * 3 + 1;
        var repeatIndex = 0;

        this.schedule(function () {
            if(repeatIndex == repeatTime-1){
                var player = this.mPlayer[hostSeat];
                if(!player){
                    return;
                }
                player.playMakeHostAni();
            }else if(repeatIndex == repeatTime){
                var player = this.mPlayer[hostSeat];
                if(!player){
                    return;
                }
                player.playHostAni();
            }else if(repeatIndex == repeatTime + 5){
                this.Panel_hostBlack.setVisible(false);
                this.setMakeHost(false);

                var newTableStatus = mo.dtjTableStatus.mTableStatus;
                newTableStatus.LastAction = null;
                mo.dtjTableStatus.setTableStatus(newTableStatus);

            }else if(repeatIndex < repeatTime- 1){
                var seat = makeHost[repeatIndex % makeHost.length];
                var player = this.mPlayer[seat];
                if(!player){
                    return;
                }
                player.playMakeHostAni();
            }

            repeatIndex ++;

        }, 0.2, repeatTime + 5);
    },

    //礼包
    setChatMsgReward : function(userChatMsg){
        var kwxRewardLayer = new KwxRewardLayer();
        this.addChild(kwxRewardLayer);
        var toPlayer = mo.dtjTableStatus.getViewSeatForUid(userChatMsg.uid);
        kwxRewardLayer.startAction(userChatMsg.msg, this.mPlayerPoss[toPlayer], toPlayer);
    }

});